package com.xk857.article.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xk857.article.req.CategoryREQ;
import com.xk857.article.service.ICategoryService;
import com.xk857.entities.Category;
import com.xk857.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author CV大魔王
 * @Description 文章模块 - 控制层
 * @date 2021/2/21 14:35
 */
@Api(value = "模块管理接口",tags = "模块管理接口")
@RestController
@RequestMapping("/pri/category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;


    @ApiOperation("分页条件查询模块接口")
    @PostMapping("/search")
    public IPage<Category> search(@RequestBody CategoryREQ req) {
        return categoryService.queryPage(req);
    }

    @ApiOperation("获取所有正常状态模块接口")
    @PostMapping("/list")
    public List<Category> list() {
        return categoryService.findAllNormal();
    }

    @ApiOperation("根据id查询模块接口")
    @ApiImplicitParam(name = "id", value = "模块id", required = true)
    @GetMapping("/{id}")
    public Category selectCategoryById(@PathVariable String id) {
        return categoryService.getById(id);
    }

    @ApiOperation("修改模块接口")
    @PutMapping
    public Result update(@RequestBody Category category) {
        categoryService.updateById(category);
        return Result.ok("更新成功");
    }

    @ApiOperation("添加模块接口")
    @PostMapping
    public Result save(@RequestBody Category category) {
        categoryService.save(category);
        return Result.ok("添加成功");
    }

    @ApiOperation("删除模块接口")
    @ApiImplicitParam(name = "id", value = "模块id", required = true)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        categoryService.removeById(id);
        return Result.ok("删除成功");
    }

    @ApiOperation("根据名称删除模块接口")
    @ApiImplicitParam(name = "name", value = "模块名称", required = true)
    @DeleteMapping("/name/{name}")
    public Result deleteByName(@PathVariable String name) {
        categoryService.removeByName(name);
        return Result.ok("删除成功");
    }

    @ApiOperation("获取所有正常状态模块和知识点的接口")
    @PostMapping("/all")
    public List<Category> findByCategoryAndLabe() {
        return categoryService.findCategoryAndLabel();
    }
}
